package com.ytt.core.api.controller.student;


import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.common.SimpleDataDTO;
import com.ytt.core.model.student.StudentDto;
import com.ytt.core.model.student.StudentEditDto;
import com.ytt.core.model.student.StudentRegisterDto;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.service.student.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = {"学生管理"})
@RestController
@RequestMapping("/api/student")
@Validated
public class StudentController {
    private  final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @ApiOperation(value = "根据ID查找学生")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('STUDENT_RETRIEVE')")
    public StudentDto findById(
            @ApiParam(value = "学生ID", required = true) @PathVariable String id) {
        return studentService.findById(id);
    }

    @ApiOperation(value = "分页查找学生")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('STUDENT_RETRIEVE')")
    public PageDataDTO<StudentDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "学生名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "学生学号") @RequestParam(value = "studentId", required = false) String studentId) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .student_id(studentId)
                .build();
        return studentService.findOnePage(page, size, sort, param);
    }

    @ApiOperation(value = "查找所有学生")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('STUDENT_RETRIEVE')")
    public List<StudentDto> findAll(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "学生名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "学生学号") @RequestParam(value = "studentId", required = false) String studentId) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .student_id(studentId)
                .build();
        return studentService.findAll(sort,param);
    }



    @ApiOperation(value = "创建学生")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('STUDENT_CREATE')")
    public StudentDto create(@ApiParam(value = "学生信息", required = true) @RequestBody @Validated StudentEditDto studentEditDto) {
        return studentService.create(studentEditDto);
    }

    @ApiOperation(value = "学生注册 不需要权限")
    @PostMapping(value = "/register")
    public StudentDto register(@ApiParam(value = "学生信息", required = true) @RequestBody @Validated StudentRegisterDto studentRegisterDto) {
        return studentService.register(studentRegisterDto);
    }

    @ApiOperation(value = "查询学生登录学号是否可用 不需要权限")
    @GetMapping(value = "/availability")
    public SimpleDataDTO<Boolean> checkStudentIdAvailability(
            @ApiParam(value = "学生ID, 新增用户时检查账号可用性可不填") @RequestParam(value = "id", required = false) String id,
            @ApiParam(value = "学号", required = true) @RequestParam(value = "studentId") String studentId) {
        return studentService.checkStudentIdAvailability(id, studentId);
    }


    @ApiOperation(value = "更新学生")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('STUDENT_UPDATE')")
    public StudentDto update(@ApiParam(value = "学生ID", required = true) @PathVariable String id,
                            @ApiParam(value = "学生信息", required = true) @RequestBody @Validated StudentEditDto studentEditDto) {
        return studentService.update(id, studentEditDto);
    }

    @ApiOperation(value = "删除学生")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('STUDENT_DELETE')")
    public void delete(@ApiParam(value = "学生ID", required = true) @PathVariable String id) {
        studentService.delete(id);
    }
}
